#include "netDataManager.h"
#include "PackDefine.h"
#include "NetPackDefine.h"
#include <QFile>
#include <QDataStream>
#include <QApplication>
#include <QDebug>
#include "LogManager.h"

void parseDataTest()
{
	QString text = QApplication::applicationDirPath() + "/tcpdata.txt";

	QFile file(text);
	if (!file.open(QFile::ReadOnly | QFile::Text))
		return;

	NetDataManager manager;
	manager.newSession(1, NULL);

	QTextStream in(&file);
	while (!in.atEnd())
	{
		QString line = in.readLine();
		if (line.startsWith("/"))
			continue;

		int s = line.indexOf(" ");
		line = line.mid(s + 1);		

		QByteArray data = QByteArray::fromHex(line.toUtf8());
		manager.recvData(1,data);
	}

	int pack_num = 0;
	while (true)
	{
		NetPackPtr pack = manager.takePack(1);
		if (!pack)
			break;

		NetPackVariant *ptr = (NetPackVariant*)pack.data();
		int cmd = ptr->info.CmdID;
		pack_num++;
		qDebug() << cmd;

		if (cmd == BTS_CMD_SEND_RTDATA76)
		{
			stru_BTS_CMD_SEND_RTDATA76 data76;
			if (data76.unpack(ptr->data))
			{
				int item_idx = (ptr->info.SubDevID - 1) * 8 + data76.ChannelIDByNum - 1;
				if (item_idx >= 0 && item_idx < 32)
				{
				}
				else
				{
					LogError("BTS_CMD_SEND_RTDATA76 sub_id = %d,chn = %d", ptr->info.SubDevID, data76.ChannelIDByNum);
				}
			}
		}
		else if (cmd == BTS_CMD_SEND_DFDATA76)
		{
			stru_BTS_CMD_SEND_DFDATA76 df76;
			if (df76.unpack(ptr->data))
			{				
				for (int i = 0; i < df76.DFDataNumber; i++)
				{
					auto &data = df76.dataList[i];
					int item_idx = (data.SubDevID - 1) * 8 + data.ChannelIDByNum - 1;
					if (item_idx >= 0 && item_idx < 32)
					{			
					}
					else
					{
						LogError("BTS_CMD_SEND_DFDATA76 sub_id = %d,chn = %d", data.SubDevID, data.ChannelIDByNum);
						break;
					}
				}
			}
		}
	}

	qDebug() << "total recv num " << pack_num;
}